package com.java.goods.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.java.common.vo.R;
import com.java.goods.entity.Category;
import com.java.goods.entity.Goods;
import com.java.goods.mapper.CategoryMapper;
import com.java.goods.mapper.GoodsMapper;
import com.java.goods.service.GoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author jiangli
 * @since 2019-12-15
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
	@Autowired
	private GoodsMapper goodsMapper;
	@Autowired
	private CategoryMapper categoryMapper;

	@Override
	public R getIndexItem() {
		// 查询所有的类型
		List<Category> listItemCat = categoryMapper.allItemCat();
		Map<String, Object> reuslt = new HashMap<>();
		for (Category category : listItemCat) {
			Long id = category.getId();
			String name = category.getName();
			//查询该分类下的商品
			List<Goods> listItem = goodsMapper.getIndexItem(id);
			if (CollUtil.isNotEmpty(listItem)) {
				reuslt.put(name, listItem);
			}

		}
		return R.ok(reuslt);
	}

	@Override
	public R geItem(@RequestParam("id") Long id) {
		Goods item = goodsMapper.getItem(id);
		if(item==null){
			return R.error("没有查询到结果");
		}
		return R.ok(item);
	}

}
